﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using FluentNHibernate.AutoMap;
using NHibernate.Cfg;
using NotMyself.Oxite.Model;

namespace NotMyself.Oxite.Data.LinqToNHibernateProvider
{
    class PersistenceConfiguration
    {
        public virtual void Configure(Configuration configuration)
        {
            var model = new AutoPersistenceModel
            {
                Conventions =
                {
                    GetForeignKeyNameOfParent = (type => type.Name + "Id"),
                    GetForeignKeyName = (info => info.Name + "Id"),
                    GetTableName = (type => Inflector.Pluralize(type.Name)),
                    GetManyToManyTableName =
                        ((child, parent) =>
                         Inflector.Pluralize(child.Name) + "To" + Inflector.Pluralize(parent.Name))
                }
            };


            model
                .AddEntityAssembly(Assembly.GetAssembly(typeof(IEntity)))
                .Where(entity =>
                    entity.Namespace.EndsWith("Model") &&
                    entity.GetProperty("ID") != null &&
                    entity.IsAbstract == false
                    )
                .Configure(configuration);
        }

    }
}
